home *** CD-ROM | disk | FTP | other *** search
- unit Delimit;
-
- interface
-
- uses SysUtils, Classes;
-
- type
- TDelimitedString = class (TObject)
- private
- fStr: String;
- fList: TStringList;
- fDelimiter: Char;
- procedure ParseString;
- procedure SetText (const NewStr: String);
- function GetFieldCount: Integer;
- function GetField (Index: Integer): String;
- procedure SetDelimiter (Delim: Char);
- public
- constructor Create;
- constructor CreateAssign (const NewStr: String);
- destructor Destroy; override;
- property FieldCount: Integer read GetFieldCount;
- property Text: String read fText write SetText;
- property Delimiter: Char read fDelimiter write SetDelimiter;
- property Field [Index: Integer]: String read GetField; default;
- end;
-
- implementation
-
- constructor TDelimitedString.Create;
- begin
- Inherited Create;
- fDelimiter := ',';
- fList := TStringList.Create;
- end;
-
- constructor TDelimitedString.CreateAssign (const NewStr: String);
- begin
- Self.Create;
- Assign (NewStr);
- end;
-
- destructor TDelimitedString.Destroy;
- begin
- fList.Free;
- Inherited Destroy;
- end;
-
- procedure TDelimitedString.ParseString;
- var
- i: Integer;
- buff: String;
- begin
- fList.Clear;
- buff := fStr;
- while Length (buff) > 0 do
- begin
- i := Pos (Delimiter, buff);
- if i = 0 then i := Length (buff) + 1;
- fList.Add (Copy (buff, 1, i - 1));
- Delete (buff, 1, i);
- end;
- end;
-
- procedure TDelimitedString.Assign (const NewStr: String);
- begin
- fStr := NewStr;
- ParseString;
- end;
-
- function TDelimitedString.GetFieldCount: Integer;
- begin
- Result := fList.Count;
- end;
-
- function TDelimitedString.GetField (Index: Integer): String;
- begin
- Result := '';
- if (Index >= 0) and (Index < fList.Count) then
- Result := fList.Strings [Index];
- end;
-
- procedure TDelimitedString.SetDelimiter (Delim: Char);
- begin
- if fDelimiter <> Delim then
- begin
- fDelimiter := Delim;
- ParseString;
- end;
- end;
-
- end.
-
-